//********************************************************************
//
//Authors : Vincent Joubert
//Project : TestAccelero
//File    : main.c
//
// *******************************************************************
#include <p18f46k22.h>
#include "Accelerometre_parabole.h"

#pragma config FOSC = HSHP 		//HS oscillator (high power > 16 MHz)
#pragma config WDTEN = OFF		//Watch dog timer is always disabled. SWDTEN has no effect. 
#pragma config LVP = OFF		//Single-Supply ICSP disabled



#define Uint8 unsigned char



#pragma code					// declare executable instructions



//Interrupt prototype
void InterruptHandlerHigh (void);

// High priority interrupt vector
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
  _asm
	goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}

signed int x, y, z;

#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh (void)
{	

	if(INTCON3bits.INT1IF){
		
		//Test entrée en interruption, allumer led_acc
		LATBbits.LATB4 = 1;

		ACC_get_xyz(&x, &y, &z);//nécessaire pour acquiter l'int coté accéléro

		INTCON3bits.INT1IF = 0;
		LATBbits.LATB4 = 0;

	}
}

void main(void){
	
	//Enable interrupts
	RCONbits.IPEN = 1;			//Enable interrupt priority levels
	INTCONbits.GIEH = 1;		//Enable all high priority interrupt
	INTCONbits.GIEL = 1;		//Enable all low priority interrupt
	
	//init led_acc
	TRISBbits.RB4 = 0;
	ANSELBbits.ANSB4 =0;
	LATBbits.LATB4 = 0;
	
	//acc interrupt --> INT2
	ANSELBbits.ANSB1 = 0;		//Congigures INT1 as digital port
	TRISBbits.RB1 = 1;			//Configures INT1 as input of MCU
	INTCON2bits.INTEDG1 = 0;	//Interrupt on falling edge
	INTCON3bits.INT1IP = 1;		//Interrupt priority high
	INTCON3bits.INT1IF = 0;		//Clear the INT1 interrupt flag
	INTCON3bits.INT1IE = 1;		//Enable the INT1 external interrupt	

	init_acc();
	
	while(1); 
}

